package com.example.streamapi.service;

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Service
public class OllamaService {
    private final WebClient webClient;
    private static final String OLLAMA_BASE_URL = "http://www.esunrising.net:11434";
    private static final String OLLAMA_MODEL = "deepseek-r1";

    public OllamaService() {
        this.webClient = WebClient.builder()
                .baseUrl(OLLAMA_BASE_URL)
                .build();
    }

    public Flux<String> streamChat(String content) {
        return webClient.post()
                .uri("/api/generate")
                .bodyValue(new OllamaRequest(OLLAMA_MODEL, content))
                .retrieve()
                .bodyToFlux(OllamaResponse.class)
                .map(response -> response.response);
    }

    private record OllamaRequest(String model, String prompt) {}
    private record OllamaResponse(String response) {}
}